
var page_cur = 1; //当前页
var total_num, page_size, page_total_num;//总记录数,每页条数,总页数

function getData(param) { //获取当前页数据
    $.ajax({
        type: 'GET',
        url: '/search/pagejson',
        data: {
            param:param
        },
        timeout:60000,
        dataType: 'text',
        success: function(json) {
            $(".product_iwrap").empty();
            var json = $.parseJSON(json);
            total_num = json.total_num;//总记录数
            page_size = json.page_size;//每页数量
            page_cur = param.page;//当前页
            page_total_num = json.page_total_num;//总页数
            $("#currPageNum").val(page_cur);
            $("#totalPages").val(page_total_num);
            $("#currPage").text(page_cur);
            $("#totalPage").text(page_total_num);
            $("#total").text(total_num);
            var li = "";
            var html = "";
            var list = json.list;
            var area = json.area;
            var param = json.param;

            for(aindex in area){
                html +="<a href='' title='"+area[aindex]+"'>"+area[aindex]+"</a>";
            }
            $(".nav_area .search_te").html(html);
            for(index in list) { //遍历返回json
                li += "<li class=\"product_item\">";
                li+="<div class=\"product_list_title\">";
                li+="<span class=\"items icons\" product_id=\""+list[index]['product_id']+"\">+</span>";
                li+="<span class=\"items breed\">"+list[index]['cate_name']+"</span>";
                li+="<span class=\"items grade\">"+list[index]['product_name']+"</span>";
                li+=" <span class=\"items firm\">"+list[index]['brand_name']+"</span>";
                li+="<span class=\"items price orange fwb\">"+list[index]['price']+"起</span>";
                li+="<span class=\"items number\">"+list[index]['num']+"</span>";
                li+="<span class=\"items supplier blue\">"+list[index]['price_num']+"家报价</span>";
                li+="<span class=\"items operate\" product_id=\""+list[index]['product_id']+"\"><a href=\"javascript:void(0)\" >查看</a></span>";
                li+="</div>";

                li+="<ul class=\"product_list_content\">";

                li+="</ul>";
                li +="<li>";
            };
            $(".product_iwrap").html(li);
        },
        complete: function() {
            getPageBar();//js生成分页，可用程序代替
        },
        error: function() {
            alert("数据异常,请检查是否json格式");
        }
    });
}

function getPageBar() { //js生成分页
    if (page_cur > page_total_num)
        page_cur = page_total_num;//当前页大于最大页数
    if (page_cur < 1)
        page_cur = 1;//当前页小于1
    page_str = "<span>共" + total_num + "条</span><span>" + page_cur + "/" + page_total_num + "</span>";
    if (page_cur == 1) {//若是第一页
        page_str += "<span>首页</span><span>上一页</span>";
    } else {
        page_str += "<span><a href='javascript:void(1)' data-page='1'>首页</a></span><span><a href='javascript:void(" + (page_cur - 1) + ")' data-page='" + (page_cur - 1) + "'>上一页</a></span>";
    }

    if (page_total_num < 10){
        for (var i = 1; i <= page_total_num; i++) {
            if (i == page_str) {
                page_str += "<span>" + i + "</span>";//当前页
            } else {
                page_str += "<span><a href='javascript:void(" + i + ")' data-page='" + i + "'>" + i + "</a></span>";
            }
        }
    }else{
        if (page_cur < 6) {
            for (var i = 1; i <= 10; i++) {
                if (i == page_cur) {
                    page_str += "<span>" + i + "</span>";//当前页
                } else {
                    page_str += "<span><a href='javascript:void(" + i + ")' data-page='" + i + "'>" + i + "</a></span>";
                }
            }
        } else {
            var cur = parseInt(page_cur) + 5;
            if (cur <= page_total_num) {
                var pre = parseInt(page_cur) - 4;
                var last = parseInt(page_cur) + 5;
                for (i = pre; i <= last; i++) {
                    if (i == page_cur) {
                        page_str += "<span>" + i + "</span>";//当前页
                    } else {
                        page_str += "<span><a href='javascript:void(" + i + ")' data-page='" + i + "'>" + i + "</a></span>";
                    }
                }
            } else {
                var pre = parseInt(page_total_num) - 9;
                for (i = pre; i <= parseInt(page_total_num); i++) {
                    if (i == page_cur) {
                        page_str += "<span>" + i + "</span>";//当前页
                    } else {
                        page_str += "<span><a href='javascript:void(" + i + ")' data-page='" + i + "'>" + i + "</a></span>";
                    }
                }
            }
        }
    }
    if (page_cur >= page_total_num) {//若是最后页
        page_str += "<span>下一页</span><span>尾页</span>";
    } else {
        page_str += "<span><a href='javascript:void(" + (parseInt(page_cur) + 1) + ")' data-page='" + (parseInt(page_cur) + 1) + "'>下一页</a></span><span><a href='javascript:void(" + page_total_num + ")' data-page='" + page_total_num + "'>尾页</a></span>";
    }


    $("#page").html(page_str);
}
/**
 *初始化数据
 */
$(function() {
    var kd = $("#kd").val();
    var  currency_name;
    var  area_name;
    var  cate_name;
    var currPageNum = parseInt($("#currPageNum").val());
    if(!currPageNum){
        currPageNum = 1;
    }
    var param = {};
    param['kd'] = kd;
    param['page'] = currPageNum;
    $('.nav_variety .search_wrap_text a').each(function(){
        if($(this).attr("is_choice")=='select'){
            cate_name = $(this).attr("cate_name");
            $(this).closest(".nav_variety").css("display","none");
        }
    });

    $('.nav_rmb .search_wrap_text a').each(function(){
        if($(this).attr("is_choice")=='select'){
            currency_name = $(this).attr("currency_name");
            $(this).closest(".nav_rmb").css("display","none");
        }
    });

    $('.nav_area .search_wrap_text a').each(function(){
        if($(this).attr("is_choice")=='select'){
            area_name = $(this).attr("area_name");
            $(this).closest(".nav_area").css("display","none");
        }
    });


    param[ 'cate_name'] = cate_name;
    param[ 'currency_name'] = currency_name;
    param[ 'area_name'] = area_name;

    getData(param);//默认 第一页




    $(document).delegate('#page a','click', function() { //live 向未来的元素添加事件处理器,不可用bind
        var page = $(this).attr("data-page");//获取当前页
        var kd = $("#kd").val();
        var params = {};
        params['kd'] = kd
        params['page'] = page
        getData(params);
    });
});

/**
 * 首页
 */
function firstPage() {
    var kd = $("#kd").val();
    var param = {};
    param['kd'] = kd
    param['page'] = 1
    getData(param);
}

/**
 * 尾页
 */
function lastPage() {
    var param = {};
    var totalPages = $("#totalPages").val();
    var kd = $("#kd").val();
    param['kd'] = kd
    param['page'] = totalPages
    getData(param);
}


/**
 * 上一页
 */
function prePage() {
    var param = {};
    var currPageNum = parseInt($("#currPageNum").val());
    var kd = $("#kd").val();
    var tempPageNum = 1;

    if (currPageNum > 0) {
        tempPageNum = currPageNum - 1;
    }
    if (tempPageNum == 0) {
        tempPageNum = 1;
    }
    param['kd'] = kd
    param['page'] = tempPageNum
    getData(param);
}

/**
 * 跳转下一页
 */
function nextPage() {
    var param = {};
    var currPageNum = parseInt($("#currPageNum").val());
    var totalPages = parseInt($("#totalPages").val());
    var kd = $("#kd").val();
    var tempPageNum;

    if (currPageNum >= totalPages) {
        tempPageNum = totalPages;
    } else {
        tempPageNum = parseInt(currPageNum) + 1;
    }
    param['kd'] = kd
    param['page'] = tempPageNum
    getData(param);
}


$(function(){
    $(document).delegate('.product_list_title .operate,.product_list_title .icons',"click", function(e) {
        e.preventDefault();
        if(!$(this).closest('.product_item').hasClass('curr')){
            var product_id = $(this).attr("product_id");
            var obj = $(this);
            $.ajax({
                type: 'GET',
                url: '/search/productjson',
                data: {
                    product_id: product_id,
                },
                dataType: 'text',
                success: function(json) {
                    var json = $.parseJSON(json);
                    var  li = "";
                    for(list_index in json) {
                        li+="<li>";
                        li+="<div class=\"plc_items\">";
                        if(json[list_index]['type']=='stock'){
                            li+="<div class=\"items plc_items_text\"><i class=\"plc_icon\">自营现货</i>";
                        }else{
                            li+="<div class=\"items plc_items_text\">"+json[list_index]['business_name'];
                        }
                        li+="<p class=\"plc_text\"><span class=\"pr5\">"+json[list_index]['contacts']+"</span><span class=\"pr5\">"+json[list_index]['mobile']+"</span><span class=\"pr5\"><a href=\"#\"><img src=\"/images/login_qq.png\" class=\"vam mt_3\" /></a></span><span class=\"green\">"+json[list_index]['delivery_time']+"</span></p>";
                        li+="</div>";
                        li+="<span class=\"items firm\">"+json[list_index]['delivery_area']+"</span>";

                        if(list_index==0){
                            li+="<span class=\"items price blue fwb\">"+json[list_index]['price']+"<i>最低</i></span>";
                        }else{
                            li+="<span class=\"items price blue fwb\">"+json[list_index]['price']+"</span>";
                        }

                        li+="<span class=\"items number\">"+json[list_index]['num']+"</span>";
                        li+="<span class=\"items time\">"+json[list_index]['update_time']+"</span>";
                        li+="<span class=\"items operate\">";
                        li+="<a href=\"#\" class=\"buy\">立即认购</a><a href=\"#\" class=\"negotiate\">委托洽谈</a>";
                        li+="</span>";
                        li+="</div>";
                        li+="</li>";
                    }

                    //$(this).closest(".product_list_title").siblings(".product_list_content").html(li);
                    obj.closest(".product_list_title").siblings(".product_list_content").html(li);
                }

            });


            $(this).closest('.product_item').addClass('curr');
            $(this).addClass('curr');
            $(this).siblings('.icons').addClass('curr');
            $(this).find('a').text('收起');
            $(this).siblings('.operate').find('a').text('收起');


        }else{

            $(this).closest('.product_item').removeClass('curr');
            $(this).removeClass('curr');
            $(this).siblings('.icons').removeClass('curr');
            $(this).find('a').text('查看');
            $(this).siblings('.operate').find('a').text('查看');

        }
    });
});

//帅选，品中
$(function(){
    var params = {};
    var  currency_name;
    var  area_name;
    $(".nav_variety .search_wrap_text a").on("click", function () {
        $(this).attr("is_choice","select");//这个判断当前选择
        var cate_name = $(this).attr("cate_name");
        var currPageNum = parseInt($("#currPageNum").val());
        var kd = $("#kd").val();

      $('.nav_rmb .search_wrap_text a').each(function(){
          if($(this).attr("is_choice")=='select'){
               currency_name = $(this).attr("currency_name");
          }
        });

        $('.nav_area .search_wrap_text a').each(function(){
            if($(this).attr("is_choice")=='select'){
                area_name = $(this).attr("area_name");
            }
        });
        $(this).closest(".nav_variety").css("display","none");
        params[ 'kd'] = kd;
        params[ 'page'] = currPageNum;
        params[ 'cate_name'] = cate_name;
        params[ 'currency_name'] = currency_name;
        params[ 'area_name'] = area_name;
        getData(params);
    });
    //这边要隐藏当前行，并在头部添加选中的产品分类



});